package com.jlt.service.dic;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.jlt.dao.dic.DicEntityDao;
import com.jlt.dao.dic.DicTypeDao;
import com.jlt.entity.dic.DicEntity;
import com.jlt.entity.dic.DicType;

/**
 * 字典相关实体的管理类,.
 * 
 * @author zhoutao
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有函数纳入事务管理.
@Transactional
public class DicTypeManager {

	private static Logger logger = LoggerFactory.getLogger(DicTypeManager.class);

	private DicTypeDao dicTypeDao;
	private DicEntityDao dicEntityDao;

	// -- DicType Manager --//
	@Transactional(readOnly = true)
	public DicType getDicType(Long id) {
		return dicTypeDao.get(id);
	}

	/**
	 * 使用属性过滤条件查询字典类型.
	 */
	@Transactional(readOnly = true)
	public Page<DicType> searchDicType(final Page<DicType> page, final List<PropertyFilter> filters) {
		return dicTypeDao.findPage(page, filters);
	}

	@Transactional(readOnly = true)
	public List<DicType> getAllDicType() {
		return dicTypeDao.getAll("id", true);
	}

	public void saveDicType(DicType entity) {
		dicTypeDao.save(entity);
	}

	public void deleteDicType(Long id) {
		logger.info("save dic type success");
		dicTypeDao.delete(id);
	}

	public Boolean isHaveDicEntity(Long typeId) {
		List<DicEntity> dicEntity = dicEntityDao.findDicEntityByTypeId(typeId);
		if (null != dicEntity && dicEntity.size() > 0) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	@Autowired
	public void setDicTypeDao(DicTypeDao dicTypeDao) {
		this.dicTypeDao = dicTypeDao;
	}

	@Autowired
	public void setDicEntityDao(DicEntityDao dicEntityDao) {
		this.dicEntityDao = dicEntityDao;
	}
}
